﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using Net.Chirash.Twitter.TwitterClient;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents
{
    /// <summary>
    /// RateLimitPresenter.xaml の相互作用ロジック
    /// </summary>
    public partial class RateLimitPresenter : UserControl
    {
        public RateLimitPresenter()
        {
            InitializeComponent();
        }

        private DispatcherTimer _timer;

        public RateLimit RateLimitData
        {
            get { return this.Resources["RateLimitData"] as RateLimit; }
            set { this.Resources["RateLimitData"] = value; }
        }

        #region Events

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _timer = new DispatcherTimer(DispatcherPriority.Normal, this.Dispatcher);
            _timer.Tick += new EventHandler((o, args) =>
                {
                    TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();
                    TwitterCommand cmd = TwitterCommandFactory.CreateRemoteTwitterCommand();
                    RateLimit limit = null;
                    Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                limit = cmd.RateLimitStatus();
                            }
                            catch (System.Net.WebException ex) { }
                        })
                        .ContinueWith((task) =>
                        {
                            try
                            {
                                if (task.Exception == null)
                                    this.RateLimitData = limit;
                            }
                            catch (Exception)
                            {
                            }
                        }, uiThread);
                });
            _timer.Interval = new TimeSpan(0, 0, 0);
            _timer.Start();
            _timer.Interval = TweetZone.Properties.Settings.Default.RateLimitRefreshInterval;
            
        }

        private void Grid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.rect != null && RateLimitData.HourlyLimit != 0)
                this.rect.Width = this.ActualWidth * ((double)RateLimitData.RemainingHits / (double)RateLimitData.HourlyLimit);
        }

        private void grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Grid_DataContextChanged(sender, new DependencyPropertyChangedEventArgs());
        }

        #endregion
    }
}
